/**
 * 
 */
package com.dhcc.android.base.common.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;


/**
 *@pengling 重写水平滚动条
 * @author pengling
 * @date 2015-5-24
 */
public class ScrollViewCustom extends HorizontalScrollView {
	private Runnable scrollerTask;
	private int intitPosition;
	private int newCheck = 100;
	private int childWidth = 0;
	private OnScrollStopListner onScrollstopListner;

	public interface OnScrollStopListner {
		void onScrollStoped();

		void onScrollToLeftEdge();

		void onScrollToRightEdge();

		void onScrollToMiddle();
	}

	public ScrollViewCustom(Context context, AttributeSet attrs) {
		super(context, attrs);
		scrollerTask = new Runnable() {
			@Override
			public void run() {
				int newPosition = getScrollX();
				if (intitPosition - newPosition == 0) {
					if (onScrollstopListner == null) {
						return;
					}
					onScrollstopListner.onScrollStoped();
					Rect outRect = new Rect();
					getDrawingRect(outRect);
					if (getScrollX() == 0) {
						onScrollstopListner.onScrollToLeftEdge();
					} else if (childWidth + getPaddingLeft()
							+ getPaddingRight() == outRect.right) {
						onScrollstopListner.onScrollToRightEdge();
					} else {
						onScrollstopListner.onScrollToMiddle();
					}
				} else {
					intitPosition = getScrollX();
					postDelayed(scrollerTask, newCheck);
				}
			}
		};
	}

	public void setOnScrollStopListner(OnScrollStopListner listner) {
		onScrollstopListner = listner;
	}

	public void startScrollerTask() {
		intitPosition = getScrollX();
		postDelayed(scrollerTask, newCheck);
		checkTotalWidth();

	}

	private void checkTotalWidth() {
		if (childWidth > 0) {
			return;
		}
		for (int i = 0; i < getChildCount(); i++) {
			childWidth += getChildAt(i).getWidth();
		}
	}
}